<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 供应商控制器
 */
class SupperController extends Controller {
	//供应商列表
	public function index()
	{
		///$yid=!empty($_SESSION['yid'])?$_SESSION['yid']:1;
		$page=I('get.page','1','strip_tags');
		$where['adminid']=$_SESSION['adminid'];
		$where['isdel']=0;

        $count=M('supper_user')->where($where)->count();
        $pagearr= getPageArr($page,C('PAGESIZE'),$count);
		$result=M('supper_user')->where($where)
		                 ->limit($pagearr['start'],$pagearr['pagesize'])
		                 ->order('supperid desc')->select();
		                 foreach($result as $k=>$v){
                         if($v['adminid']!=0){
                         	$result[$k]['yname']='总公司供应商';
                         	
                         }else{
                         	$result[$k]['yname']=M('yunying_user')->where("yid='%s'",$v['yid'])->getField("yname");
                         	
                         }

		                 }
		$this->assign('suppers',$result);
		$this->assign('pagearr',$pagearr);
		$this->display('index');
	}
	/*
	 *编辑供应商
	 *@param supperid 
	 *@param yid 运营商id
	 */
	public function editSupper()
	{
		if(IS_POST)
		{
			 $data['qq']=I('post.qq','','strip_tags');
		     $data['secondcontact']=I('post.secondcontact','','strip_tags');
			 $data['name']=I('post.name','','strip_tags');
			 $data['contactname']=I('post.contactname','','strip_tags');
			 $data['companyname']=I('post.companyname','','strip_tags');
			 $data['mobile']=I('post.mobile','','strip_tags');
			 $data['telephone']=I('post.telephone','','strip_tags');
			 $data['email']=I('post.email','','strip_tags');
			 $data['address']=I('post.address','','strip_tags');
			 $pass=I('post.pass','','strip_tags');

			 if (!empty($pass)) 
			 {
			 	$data['pass']=md5($pass);
			 }
			  $data['lasttime']=time();
			  $where['supperid']=$_GET['supperid'];
			  $result=M('supper_user')->where($where)->save($data);
		
			  if($result !== false)
			  {
			  	$this->success('修改供应商成功');
			  }

		}else
			{
			$where['supperid']=$_GET['supperid'];
			$supperinfo=M('supper_user')->where($where)->select();
			$this->assign('supper',$supperinfo);
		  	$this->display();
			}
	}

	/*
	 *添加供货商
	 */
	public function addSupper(){

		$data['adminid']=$_SESSION['adminid'];
		$data['qq']=I('post.qq','','strip_tags');
		$data['secondcontact']=I('post.secondcontact','','strip_tags');
		$data['regtime']=time();
		$data['name']=I('post.name','','strip_tags');
		$data['contactname']=I('post.contactname','','strip_tags');
		$data['companyname']=I('post.companyname','','strip_tags');
		$data['mobile']=I('post.mobile','','strip_tags');
		$data['telephone']=I('post.telephone','','strip_tags');
		$data['email']=I('post.email','','strip_tags');
		$data['address']=I('post.address','','strip_tags');
		$data['pass']=I('post.pass','','strip_tags');
		$data['pass']=md5(trim($data['pass']));

		$result=M('supper_user')->add($data);
		 if(!empty($result))
		 {
		 	$this->success('添加供应商成功');
		 }else{
		 	$this->success('添加供应商失败，请检测信息是否完全！');
		 }

	}


	/**
	 * 添加供应商静态显示
	 */
	public function add(){
		$this->display("addSupper");
	}
	
	


	 public function weiyi()
	{
		$supper=M('supper_user');
		$action=I('post.action','','strip_tags');
		switch ($action) {
			case 'editsup':
				$supperid=I('post.supperid',0,'strip_tags');
				$where['companyname']=I('post.companyname','','strip_tags');

				$where['supperid']=array('neq',$supperid);
				$map['name']=I('post.name','','strip_tags');
				$map['supperid']=array('neq',$supperid);
			
				$result1=$supper->where($where)->count();

				$result2=$supper->where($map)->count();

				break;
			case 'addsup':
				$where['companyname']=I('post.companyname','','strip_tags');
				$map['name']=I('post.name','','strip_tags');
				$result1=$supper->where($where)->count();
				$result2=$supper->where($map)->count();
				break;
			
		}
		if($result1 > 0 || $result2 > 0)
		{
					$res['status']=0;
					$res['msg']='登录用户名或公司名称被占用';
					echo json_encode($res,JSON_UNESCAPED_UNICODE);
					return '';
		}
		$res['status']=1;
		echo json_encode($res,JSON_UNESCAPED_UNICODE);

	}
	
	
	
}